package labox.innovation.gameserver.script.faenor;

import javax.script.ScriptContext;

import labox.innovation.config.Config;
import labox.innovation.gameserver.script.Parser;
import labox.innovation.gameserver.script.ParserFactory;
import labox.innovation.gameserver.script.ScriptEngine;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.w3c.dom.Node;

/**
 * @author Luis Arias
 *
 */
public class FaenorQuestParser extends FaenorParser
{
    protected static final Logger _log = LoggerFactory.getLogger(FaenorQuestParser.class.getName());
    @Override
	public void parseScript(Node questNode, ScriptContext context)
    {
        if (DEBUG) 
            _log.info("Parsing Quest.", Config.SERVER_ID, "�ű�����");

        String questID = attribute(questNode, "ID");

        for (Node node = questNode.getFirstChild(); node != null; node = node.getNextSibling()) {
            if (isNodeName(node, "DROPLIST"))
            {
                parseQuestDropList(node.cloneNode(true), questID);
            }
            else if (isNodeName(node, "DIALOG WINDOWS"))
            {
                //parseDialogWindows(node.cloneNode(true));
            }
            else if (isNodeName(node, "INITIATOR"))
            {
                //parseInitiator(node.cloneNode(true));
            }
            else if (isNodeName(node, "STATE"))
            {
                //parseState(node.cloneNode(true));
            }
        }
    }

    private void parseQuestDropList(Node dropList, String questID) throws NullPointerException
    {
        if (DEBUG) 
            _log.info("Parsing Droplist.", Config.SERVER_ID, "�ű�����");

        for (Node node = dropList.getFirstChild(); node != null; node = node.getNextSibling()) {
            if (isNodeName(node, "DROP"))
            {
                parseQuestDrop(node.cloneNode(true), questID);
            }
        }
    }

    private void parseQuestDrop(Node drop, String questID)// throws NullPointerException
    {
        if (DEBUG) 
            _log.info("Parsing Drop.", Config.SERVER_ID, "�ű�����");

        int npcID;
        int itemID;
        int min;
        int max;
        int chance;
        String[] states;
        try
        {
            npcID = getInt(attribute(drop, "NpcID"));
            itemID = getInt(attribute(drop, "ItemID"));
            min = getInt(attribute(drop, "Min"));
            max = getInt(attribute(drop, "Max"));
            chance = getInt(attribute(drop, "Chance"));
            states = (attribute(drop, "States")).split(",");
        }
        catch (NullPointerException e)
        {
            throw new NullPointerException("Incorrect Drop Data");
        }

        if (DEBUG) 
            _log.info("Adding Drop to NpcID: "+npcID, Config.SERVER_ID, "�ű�����");

        _bridge.addQuestDrop(npcID, itemID, min, max, chance, questID, states);
    }

    static class FaenorQuestParserFactory extends ParserFactory
    {
        @Override
		public Parser create()
        {
            return(new FaenorQuestParser());
        }
    }

    static
    {
        ScriptEngine.parserFactories.put(getParserName("Quest"), new FaenorQuestParserFactory());
    }
}
